package com.itemp.wifidirectsend;

import android.content.Context;
import android.net.wifi.p2p.WifiP2pDevice;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import java.util.List;

public class DeviceAdapter extends BaseAdapter {

    private LayoutInflater inflater;
    private List<WifiP2pDevice> wifiP2pDeviceList;
    private OnItemClickListener onItemClickListener;

    public DeviceAdapter(Context context, List<WifiP2pDevice> wifiP2pDeviceList) {
        this.inflater = LayoutInflater.from(context);
        this.wifiP2pDeviceList = wifiP2pDeviceList;

    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }


    @Override
    public int getCount() {
        return wifiP2pDeviceList.size();
    }

    @Override
    public Object getItem(int i) {
        return null;
    }

    @Override
    public long getItemId(int i) {
        return 0;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.layout_item, null);
            holder = new ViewHolder();
            holder.deviceName = (TextView) convertView.findViewById(R.id.device_name);
            holder.deviceAddress = (TextView) convertView.findViewById(R.id.device_address);
            holder.deviceDetails = (TextView) convertView.findViewById(R.id.device_details);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.deviceName.setText(wifiP2pDeviceList.get(position).deviceName);
        holder.deviceAddress.setText(wifiP2pDeviceList.get(position).deviceAddress);
        holder.deviceDetails.setText(WifiP2pUtils.getDeviceStatus(wifiP2pDeviceList.get(position).status));
        convertView.setOnClickListener(v -> {
            if (null != onItemClickListener) {
                onItemClickListener.onItemClick(position);
            }
        });
        return convertView;
    }

    class ViewHolder {
        TextView deviceName;
        TextView deviceAddress;
        TextView deviceDetails;
    }

}

interface OnItemClickListener {
    void onItemClick(int position);
}

